package proxies.classes.dashboardmessages
{
	import core.events.PutIoErrorEvent;
	import core.events.PutIoResultEvent;
	
	import proxies.Proxy;
	import proxies.classes.dashboardmessages.events.DashboardMessagesError;
	import proxies.classes.dashboardmessages.events.DashboardMessagesListResponse;
	
	public class DashboardMessagesProxy extends Proxy implements IDashboardMessagesProxy
	{
		
		public static const NAME:String = "messages";
		
		public static const METHOD_LIST:String = 'list';
		public static const METHOD_DELETE:String = 'delete';
		
		public function DashboardMessagesProxy()
		{
			super(NAME);
		}
		
		public function listDashboardMessages():void
		{
			sendRequest(METHOD_LIST, onListResult, onError);
		}
		
		public function deleteDashboardMessage(id:String):void
		{
			var parameters:Object = new Object();
			parameters.id = id;
			
			sendRequest(METHOD_DELETE, onDeleteResult, onError, parameters);
		}
		
		private function onError(error:PutIoErrorEvent):void
		{
			//Add error message
			var dashboardMessagesError:DashboardMessagesError = new DashboardMessagesError(error.errorMessage, error.bubbles, error.cancelable);
			
			//Fire event
			dispatchEvent(dashboardMessagesError);
		}
		
		private function onListResult(result:PutIoResultEvent):void
		{
			
			// Build the object
			var dashboardMessages:Array = new Array();
			var currentItem:DashboardMessagesItem;
			
			for(var i:int=0; i < result.nbResults; i++)
			{		
				currentItem = new DashboardMessagesItem(result.results[i]);
				dashboardMessages.push(currentItem);
			}
			
			// Packaging. Put data into an Event.
			var dashboardMessagesListResponse:DashboardMessagesListResponse = new DashboardMessagesListResponse(dashboardMessages,result.bubbles, result.cancelable);
			
			// Fire event
			dispatchEvent(dashboardMessagesListResponse);
		}
		
		private function onDeleteResult(result:PutIoResultEvent):void
		{
			// Useless
		}
	}
}